#pragma once
#ifndef MYMATRIX_H
#define MYMATRIX_H
#include "stdio.h"
#include <cmath>

class MyMatrix{


public:
	
	static float *getIdentityMatrix() {
		float *matrix = new float[16];
		for(int i=0; i<16; i++){
			matrix[i] = 0;
		}
		matrix[0] = 1.0;
		matrix[5] = 1.0;
		matrix[10] = 1.0;
		matrix[15] = 1.0;
		return matrix;
	}
	
	static float *getNullMatrix() {
		
		float *matrix = new float[16];
		for(int i=0; i<16; i++){
			matrix[i] = 0;
		}
	
		return matrix;
	}

	static float *invertMatrix(float * matrix) {	

		//MyMatrix::printMatrix(matrix);
		float ratio,a;
		int i, j, k, n = 4;
		float *helpMatrix = getIdentityMatrix();
		for(i = 0; i < n; i++){
			for(j = 0; j < n; j++){
				if(i!=j){
					ratio = matrix[4*j+i]/matrix[4*i+i];
					for(k = 0; k < n; k++){
						matrix[4*j+k] -= ratio * matrix[4*i+k];
						helpMatrix[4*j+k] -= ratio * helpMatrix[4*i+k];
					}
				}
			}
		}
		for(i = 0; i < n; i++){
			a = matrix[4*i+i];
			for(j = 0; j < n; j++){
				helpMatrix[4*i+j] /= a;
			}
		}

		//printf("inverze \n");
		//MyMatrix::printMatrix(helpMatrix);

		
		return helpMatrix ; // matrix is regular .. inversion has been succesfull	
	}

	static void printMatrix(float *matrix) {
	
		printf("%e %e %e %e \n",matrix[0],matrix[1],matrix[2],matrix[3]);
		printf("%e %e %e %e \n",matrix[4],matrix[5],matrix[6],matrix[7]);
		printf("%e %e %e %e \n",matrix[8],matrix[9],matrix[10],matrix[11]);
		printf("%e %e %e %e \n",matrix[12],matrix[13],matrix[14],matrix[15]);
		printf("\n \n ");
	}

	
};
#endif
